<template>
    <el-dialog :title="title" :visible.sync="dialogVisible" width="700px" append-to-body>
        <el-form ref="form" :model="model" :rules="rules" inline label-width="170px" label-suffix=":" class="form-class" v-loading="loading">
            <el-form-item label="店铺名称" prop="shop_name">
                <el-input v-model.trim="model.shop_name" placeholder="请输入店铺名称" clearable></el-input>
            </el-form-item>
            <el-form-item label="店铺邮箱" prop="email">
                <el-input v-model.trim="model.email" placeholder="请输入店铺邮箱" clearable></el-input>
            </el-form-item>
            <el-form-item label="站点" prop="site">
                <!-- <el-input v-model.trim="model.site" placeholder="请输入站点" clearable></el-input> -->
                <el-select v-model="model.site" placeholder="请选择站点" clearable size="small" style="width: 240px">
                    <el-option v-for="(dict, index) in siteList" :key="index" :label="dict.warehouse_name" :value="dict.warehouse_name" />
                </el-select>
            </el-form-item>
            
            <el-form-item label="店铺类型" prop="type">
                <el-radio-group v-model="model.type">
                    <el-radio-button label="shopify">shopify</el-radio-button>
                    <el-radio-button label="shopline">shopline</el-radio-button>
                </el-radio-group>
            </el-form-item>

            <el-form-item label="状态" prop="status">
                <el-select v-model="model.status" placeholder="请选择状态" clearable>
                    <el-option v-for="(dict, index) in STATUS_LIST" :key="index" :label="dict.label" :value="dict.value" />
                </el-select>
            </el-form-item>

            <el-form-item label="设置API密钥" prop="api">
                <el-switch v-model="model.api" ></el-switch>
            </el-form-item>

            <el-form-item v-if="model.api" label="域名" prop="myshopify_domain">
                <!-- 备注 -->
                <el-tooltip class="item" effect="dark" content="请输入店铺的域名，例如：https://demo.myshopify.com" placement="top">
                    <i class="el-icon-info" style="color: #606266"></i>
                </el-tooltip>
                <el-input v-model.trim="model.myshopify_domain" placeholder="请输入域名" clearable></el-input>
            </el-form-item>
            <el-form-item v-if="model.api && model.type != 'shoplazza'" label="app_key" prop="app_key">
                <el-input v-model.trim="model.app_key" placeholder="请输入app_key" clearable></el-input>
            </el-form-item>
            <el-form-item v-if="model.api" label="app_secret" prop="app_secret">
                <el-input v-model.trim="model.app_secret" placeholder="请输入app_secret" clearable></el-input>
            </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" :loading="save" :disabled="save" @click="submit">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
        </div>
    </el-dialog>
</template>

<script>
import { editShop,addShop, getSiteList } from '@/api/shop'
import { STATUS_LIST } from '../constant'
export default {
    name: 'DetailDialog',
    props: {
        value: {
            type: Boolean,
            default: false,
            required: true,
        },
        params: {
            type: Object,
            default: () => {},
        },
    },
    data() {
        return {
            loading: false,
            save: false,
            model: {
                id: '',
                shop_name: '',
                email: '',
                supply_link: '',
                site: '',
                status: '',
                type: "shopify",
                api: 0,
            },
            rules: {
                shop_name: [{ required: true, message: '店铺名称不能为空', trigger: ['blur', 'change'] }],
                myshopify_domain: [{ required: true, message: '域名不能为空', trigger: ['blur', 'change'] }],
            },
            STATUS_LIST,
            siteList: [],
        }
    },
    computed: {
        title() {
            return `${this.model.id ? '编辑' : '新增'}店铺`
        },
        dialogVisible: {
            get() {
                return this.value
            },
            set(val) {
                this.$emit('input', val)
            },
        },
    },
    watch: {
        params: {
            handler(val) {
                if (val.id) {
                    console.log('test',val);
                    
                    this.model = Object.assign({}, this.model, val)
                }
            },
            immediate: true,
            deep: true,
        },
    },
    async created() {
        const siteData = await getSiteList()
        this.siteList = siteData?.data ?? []
    },
    methods: {
        submit() {
            this.$refs.form.validate(valid => {
                if (!valid) {
                    return
                }

                const params = { ...this.model }
                if (this.save) {
                    return
                }
                this.save = true
                const request = this.model.id ? editShop : addShop
                request(params)
                    .then(res => {
                        this.$message.success(res.msg)
                        this.dialogVisible = false
                        this.$emit('success')
                    })
                    .finally(() => {
                        this.save = false
                    })
            })
        },
        cancel() {
            this.dialogVisible = false
        },
    },
}
</script>

<style>
.form-class.el-form .el-form-item {
    width: 100%;
    margin-right: 0;
}
.form-class.el-form .el-form-item .el-input {
    width: 400px;
}
</style>
